package com.leather.express.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.leather.common.entity.Express;
import com.leather.common.entity.User;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.BeanKit;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.vo.MessageVo;
import com.leather.express.config.MQConfig;
import com.leather.express.feignclient.UserClient;
import com.leather.express.mapper.ExpressMapper;
import com.leather.express.service.ExpressSevice;
import com.leather.express.vo.ExpressQueryVo;
import com.leather.express.vo.ExpressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ExpressServiceImpl extends ServiceImpl<ExpressMapper, Express> implements ExpressSevice {


    @Autowired
    private AmqpTemplate amqpTemplate;


    @Autowired
    private ExpressMapper expressMapper;

    @Autowired
    private UserClient userClient;

    @Override
    public Result addExpress(ExpressVo expressVo) {
        Result result = expressVo.validate();
        if (!result.isSuccess()) {
            return result;
        }
        Express express = (Express) result.getData();
        CheckKit.check(save(express), CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result expressIndex(int pageId) {
        return Result.success(expressMapper.queryIndex(pageId));
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Result getExpress(int expressId, int userId) {
        CheckKit.check(expressId > 0, CodeMsg.EMPTY_EXPRESS_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        //判断该ID是否已被领取
        Express express = expressMapper.getExpressById(expressId);
        CheckKit.check(express != null, CodeMsg.IS_DELETE);
        CheckKit.check(express.getFlag() != 1, CodeMsg.IS_GET);
        User nowUser = userClient.getUserById(userId);
        User sendUser = userClient.getUserById(express.getUserId());
        CheckKit.check(nowUser != null, CodeMsg.ERROR_USER_ID);
        int count = expressMapper.updateExpressById(express.getId(),userId);
        CheckKit.check(count>0, CodeMsg.IS_GET);
        String info = nowUser.getRealName() + "-" + nowUser.getCollege() + "-" + nowUser.getPhone();
        MessageVo messageVo = new MessageVo();
        messageVo.setMessageType(0);
        messageVo.setMessageTitle("[快递领取通知]");
        messageVo.setMessageContent("你的快递代取任务已被领取,领取人信息:" + info);
        messageVo.setMessageUrl("/pages/usercenter/myexpress");
        messageVo.setUserId(express.getUserId());
        //发送消息提醒
        amqpTemplate.convertAndSend(MQConfig.MESSAGE_QUENE, BeanKit.beanToString(messageVo));
        String message = sendUser.getPhone() + "-" + info;
        //发送短信
        amqpTemplate.convertAndSend(MQConfig.MOBILE_MESSAGE_QUENE, message);
        return Result.success(getResult(express, sendUser.getPhone()));
    }

    @Override
    public Result queryUserExpress(int pageId, int userId) {
        List<ExpressQueryVo> expresses = expressMapper.queryUserExpress(pageId, userId);
        return Result.success(expresses);
    }

    @Override
    public Result queryUserGetExpress(int pageId, int userId) {
        List<ExpressQueryVo> expresses = expressMapper.queryUserOrder(pageId, userId);
        return Result.success(expresses);
    }

    @Override
    public Result getSuccess(int expressId) {
        CheckKit.check(expressId > 0, CodeMsg.EMPTY_EXPRESS_ID);
        Express express = getById(expressId);
        User sendUser = userClient.getUserById(express.getUserId());
        return Result.success(getResult(express, sendUser.getPhone()));
    }

    @Override
    public Result searchValue(String search) {
        if (StringUtils.isBlank(search)) {
            return Result.success(Lists.newArrayList());
        }
        List<ExpressQueryVo> searchs = expressMapper.search(search);
        return Result.success(searchs);
    }

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public Result deleteUserExpress(int expressId, int userId) {
        CheckKit.check(expressId > 0, CodeMsg.EMPTY_EXPRESS_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        Express express = getById(expressId);
        if (express.getFlag() == 1) {
            return Result.error(CodeMsg.IS_GET);
        }
        int count = expressMapper.delelteExpressById(expressId,userId);
        CheckKit.check(count>0, CodeMsg.IS_GET);
        return Result.success("OK");
    }

    @Override
    public Result getExpressById(int expressId) {
        CheckKit.check(expressId > 0, CodeMsg.EMPTY_EXPRESS_ID);
        return Result.success(expressMapper.getExpressById(expressId));
    }


    public Map<String, Object> getResult(Express express, String phone) {
        Map<String, Object> maps = Maps.newHashMap();
        maps.put("phone", express.getPhone());
        maps.put("code", express.getCode());
        maps.put("name", express.getName());
        maps.put("message", phone);
        return maps;
    }
}
